package com.demo.systemmonitor;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

/**
 * 悬浮窗控制服务
 * 通过Intent参数控制悬浮窗的启动和关闭
 */
public class FloatingWindowControlService extends Service {
    private static final String TAG = "FloatingWindowControlService";
    
    // Intent参数常量
    public static final String EXTRA_OPEN_FLOAT_WINDOW = "openfloatwindow";
 
    
    // 静态悬浮窗实例
    private static FloatingMonitorWindow floatingWindow = null;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "FloatingWindowControlService created");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "FloatingWindowControlService started with intent: " + intent);
        
        if (intent != null) {
            handleIntent(intent);
        }
        // 服务执行完毕后自动停止
        stopSelf();
        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "FloatingWindowControlService destroyed");
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    /**
     * 处理Intent参数
     */
    private void handleIntent(Intent intent) {
        String openWindow = intent.getStringExtra(EXTRA_OPEN_FLOAT_WINDOW);
        if (openWindow == null) {
            openWindow = "largewindow"; // 默认值
        }
        Log.d(TAG, "Intent parameters - openWindow: " + openWindow);
        
        if (openWindow.contains("close")) {
            Log.d(TAG, "Closing floating window");
            closeFloatingWindow();
            return;
        } else if (openWindow.contains("smallwindow")) {
            Log.d(TAG, "Starting small floating window");
            startSmallFloatingWindow();
            return;
        } else if (openWindow.contains("largewindow")) {
            Log.d(TAG, "Starting large floating window");
            startLargeFloatingWindow();
        } else {
            Log.e(TAG, "ERROR INPUT: " + openWindow);
        }
    }

    /**
     * 启动小悬浮窗
     */
    private void startSmallFloatingWindow() {
        try {
            // 先关闭现有窗口
            closeFloatingWindow();
            
            // 创建新的小窗口
            floatingWindow = new FloatingMonitorWindow();
            floatingWindow.startSmallFloatingWindow(this);
            
            Log.d(TAG, "Small floating window started successfully");
        } catch (Exception e) {
            Log.e(TAG, "Error starting small floating window", e);
        }
    }

    /**
     * 启动大悬浮窗
     */
    private void startLargeFloatingWindow() {
        try {
            // 先关闭现有窗口
            closeFloatingWindow();
            
            // 创建新的大窗口
            floatingWindow = new FloatingMonitorWindow();
            floatingWindow.startFloatingWindow(this);
            
            Log.d(TAG, "Large floating window started successfully");
        } catch (Exception e) {
            Log.e(TAG, "Error starting large floating window", e);
        }
    }

    /**
     * 关闭悬浮窗
     */
    private void closeFloatingWindow() {
        if (floatingWindow != null) {
            try {
                floatingWindow.hideFloatingWindow();
                floatingWindow = null;
                Log.d(TAG, "Floating window closed successfully");
            } catch (Exception e) {
                Log.e(TAG, "Error closing floating window", e);
            }
        } else {
            Log.d(TAG, "No floating window to close");
        }
    }

    /**
     * 获取当前悬浮窗实例（供外部调用）
     */
    public static FloatingMonitorWindow getCurrentFloatingWindow() {
        return floatingWindow;
    }

    /**
     * 检查悬浮窗是否正在显示
     */
    public static boolean isFloatingWindowVisible() {
        return floatingWindow != null;
    }
}
